﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OrangePrj
{
    public partial class FormLogin : Form
    {
        private bool authorization = false;
        public String userName;
        public String password;
        public bool admin;

        public bool getAuthorization(){
            return authorization;
        }
        public FormLogin()
        {
            InitializeComponent();
            
            authorization = false;
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            authorization = false;
            this.Close();
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (tbPassword.Text == "" || tbUserName.Text == "")
            {
                this.Height = 203;
                this.incorrect.Text = "Вы должны заполнить все поля";
                this.incorrect.Show();
                if (tbUserName.Text == "")
                {
                    tbUserName.Focus();
                }
                else
                {
                    tbPassword.Focus();
                }
                
            } 
            else 
            {
                           
                try
                {
                    String text1;
                    String text2;
                    String text3;
                    String path = Directory.GetCurrentDirectory();

                    //Расшифровка
                    int bytesRead;
                    const int bufSize = 1024;
                    
                    Stream input = File.OpenRead(path + @"\Users");
                    Stream outputStream = File.OpenWrite(path + @"\Temp1");

                    byte[] buffer = new byte[bufSize];
                    while ((bytesRead = input.Read(buffer, 0, bufSize)) > 0)
                    {
                        for (int i = 0; i < bufSize; i++)
                            buffer[i] -= 20;

                        outputStream.Write(buffer, 0, bytesRead);
                    }
                    input.Close();
                    outputStream.Close();

                    //Считывание данных о пользователях
                    FileInfo f = new FileInfo(path + @"\Temp1");
                    StreamReader inputStream = f.OpenText();

                    while ((text1 = inputStream.ReadLine()) != null &&
                        (text2 = inputStream.ReadLine()) != null &&
                        (text3 = inputStream.ReadLine()) != null)
                    {
                        if (text1.ToLower().CompareTo(tbUserName.Text.ToLower()) == 0 &&
                            text2.ToLower().CompareTo(tbPassword.Text.ToLower()) == 0)
                        {
                            //Аутентификация
                            userName = text1.ToLower();
                            password = text2.ToLower();

                            if (text3.CompareTo("1") == 0)
                                this.admin = true;
                            else
                                this.admin = false;

                            authorization = true;
                            this.Close();
                            break;
                        }
                        else
                        {
                            authorization = false;
                        }
                    }

                    if (!authorization)
                    {
                        this.Height = 203;
                        this.incorrect.Text = "Неверное имя пользователя или пароль!";
                        this.incorrect.Show();
                        tbPassword.Focus();
                        tbPassword.SelectAll();
                    }

                    inputStream.Close();
                    f.Delete();
                }
                catch (FileNotFoundException)
                {
                    this.Height = 203;
                    this.incorrect.Text = "Ошибка чтения файла! Обратитесь к Администратору!";
                    this.incorrect.Location = new Point(31, 145);
                    this.incorrect.Show();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void FormLogin_Shown(object sender, EventArgs e)
        {
            tbUserName.Focus();
        }
        private void tbPassword_Click(object sender, EventArgs e)
        {
            tbPassword.SelectAll();
        }   
    }
}
